package cn.wjdiankong.levideo.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

import com.ss.android.common.applog.GlobalContext;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import cn.wjdiankong.levideo.database.LocalStorage;

public class FileUtils {

	private final static String DATAFILE_NAME = "levideo_data.bin";

	public static boolean isDeleteSucc(int type){
		boolean isSucc = FileUtils.deleteVideoData(type);
		if(isSucc){
			isSucc = LocalStorage.getInstance().deleteLevideoData(type);
		}
		return isSucc;
	}

	public static int getVideoTypeCacheSize(int type){
		File[] files = getLocalVideo(type);
		int size = 0;
		if(files != null && files.length > 0){
			for(File file : files){
				size += file.length();
			}
		}
		return size;
	}

	public static boolean deleteVideoData(int type){
		File dirFile = new File(getSaveVideoPath(GlobalContext.getContext(), type));
		deleteAllFiles(dirFile);
		return true;
	}

	public static boolean deleteAllData(){
		return deleteVideoData(Const.VIDEO_TYPE_DOUYIN) 
				&& deleteVideoData(Const.VIDEO_TYPE_HUOSHAN) 
				&& deleteVideoData(Const.VIDEO_TYPE_KUAISHOU)
				&& deleteVideoData(Const.VIDEO_TYPE_MIAOPAI)
				&& deleteVideoData(Const.VIDEO_TYPE_TOUTIAO)
				&& deleteImgCacheData();
	}

	public static boolean deleteImgCacheData(){
		File dirFile = new File(getCacheImgPath(GlobalContext.getContext()));
		deleteAllFiles(dirFile);
		return true;
	}

	public static void deleteAllFiles(File root) {  
		File files[] = root.listFiles(); 
		if(files == null){
			return;
		}
		for (File file : files) {  
			if (file.isDirectory()) { // 判断是否为文件夹  
				deleteAllFiles(file);  
				try {
					file.delete();
				} catch (Exception e) {
				}  
			} else {  
				if (file.exists()) { // 判断是否存在  
					deleteAllFiles(file);  
					try {
						file.delete();  
					} catch (Exception e) { 
					}  
				}  
			}
		} 
	}

	public static int getImgCacheSize(){
		File[] files = new File(getCacheImgPath(GlobalContext.getContext())).listFiles();
		int size = 0;
		if(files != null && files.length > 0){
			for(File file : files){
				size += file.length();
			}
		}
		return size;
	}

	public static File[] getLocalVideo(int type){
		try{
			File videoDir = new File(getSaveVideoPath(GlobalContext.getContext(), type));
			if(videoDir.exists() || videoDir.isDirectory()){
				File[] listFile = videoDir.listFiles();
				return listFile;
			}
		}catch(Exception e){
			return null;
		}
		return null;
	}

	public static String saveBitmap(Context ctx, Bitmap bitmap, String name){
		BufferedOutputStream bos = null;
		String path = getSaveVideoPath(ctx, Const.VIDEO_TYPE_TOUTIAO);
		File dir = new File(path);
		if(!dir.exists()){
			dir.mkdirs();
		}
		File file = new File(path + name);
		try{
			bos = new BufferedOutputStream(new FileOutputStream(file));
			bitmap.compress(Bitmap.CompressFormat.PNG, 80, bos);
			bos.flush();
			return file.getAbsolutePath();
		}catch(Exception e){
			Logger.log("save bitmap err:"+Log.getStackTraceString(e));
		}finally{
			try {
				bos.close();
			} catch (Exception e) {
			}
		}
		return null;
	}

	public static String getCacheImgPath(Context ctx){
		return ctx.getExternalFilesDir(null).getAbsolutePath()+Const.LOCAL_IMG_DIR;
	}

	public static String getSaveVideoFileName(Context ctx, int type, String name){
		return getSaveVideoPath(ctx, type) + name + ".mp4";
	}

	public static String getSaveVideoPath(Context ctx, int type){
		String filePath = ctx.getExternalFilesDir(null).getAbsolutePath();
		switch(type){
		case Const.VIDEO_TYPE_DOUYIN:
			filePath = filePath + Const.LOCAL_VIDEO_DIR_DOUYIN ;
			break;
		case Const.VIDEO_TYPE_HUOSHAN:
			filePath = filePath + Const.LOCAL_VIDEO_DIR_HUOSHAN;
			break;
		case Const.VIDEO_TYPE_KUAISHOU:
			filePath = filePath + Const.LOCAL_VIDEO_DIR_KUAISHOU;
			break;
		case Const.VIDEO_TYPE_MIAOPAI:
			filePath = filePath + Const.LOCAL_VIDEO_DIR_MIAOPAI;
			break;
		case Const.VIDEO_TYPE_TOUTIAO:
			filePath = filePath + Const.LOCAL_VIDEO_DIR_TOUTIAO;
			break;
		default:
			filePath = filePath + Const.LOCAL_VIDEO_DIR_DEFAULT;
		}
		return filePath;
	}

	public static void writeData(Context ctx, String str){
		FileWriter fw = null;
		String filePath = ctx.getFilesDir() + File.separator + DATAFILE_NAME;
		try{
			fw = new FileWriter(filePath);
			fw.write(str);
			fw.flush();
		}catch(Exception e){
			Logger.log("write data err:"+Log.getStackTraceString(e));
		}finally{
			if(fw != null){
				try{
					fw.close();
				}catch(Exception e){
					Logger.log("close io err:"+Log.getStackTraceString(e));
				}
			}
		}
	}

	public static void clearFile(Context ctx){
		String filePath = ctx.getFilesDir() + File.separator + DATAFILE_NAME;
		File file = new File(filePath);
		if(file.exists()){
			file.delete();
			file = null;
		}
	}

}
